/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#ifndef PLPROCESSOR_PRIVATE_H
#define PLPROCESSOR_PRIVATE_H

#include <machine/pc/processor/x86/processorstate.h>

/**
 * The x86 implementation of the processor.
 */
class PLProcessorPrivate {
public:
	/**
	 * Constructor.
	 */
	PLProcessorPrivate();
	/**
	 * Init the processor.
	 */
	void start();
	/**
	 * Start other things after the memory management was started.
	 */
	void start1();
	/**
	 * Read a byte (8bit) from the I/O Port or the memory-mapped I/O region.
	 *
	 * @param offset offset from the I/O base port or the I/O base memory address.
	 * @return the byte (8bit) that have been read.
	 */
	inline uint8_t in8(size_t offset);
	/**
	 * Read two byte (16bit) from the I/O Port or the memory-mapped I/O region.
	 *
	 * @param offset offset from the I/O base port or the I/O base memory address.
	 * @return the two byte (16bit) that have been read.
	 */
	inline uint16_t in16(size_t offset);
	/**
	 * Read four byte (32bit) from the I/O Port or the memory-mapped I/O region.
	 *
	 * @param offset offset from the I/O base port or the I/O base memory address.
	 * @return the four byte (32bit) that have been read.
	 */
	inline uint32_t in32(size_t offset);
	/**
	 * Write a byte (8bit) to the I/O port or the memory-mapped I/O region.
	 *
	 * @param value the value that should be written.
	 * @param offset offset from the I/O base port or the I/O base memory address.
	 */
	inline void out8(uint8_t value, size_t offset);
	/**
	 * Write two byte (16bit) to the I/O port or the memory-mapped I/O region.
	 *
	 * @param value the value that should be written.
	 * @param offset offset from the I/O base port or the I/O base memory address.
	 */
	inline void out16(uint16_t value, size_t offset);
	/**
	 * Write four byte (32bit) to the I/O port or the memory-mapped I/O region.
	 *
	 * @param value the value that should be written.
	 * @param offset offset from the I/O base port or the I/O base memory address.
	 */
	inline void out32(uint32_t value, size_t offset);
};

uint8_t PLProcessorPrivate::in8(size_t offset) {
	uint8_t value;
	asm volatile("inb %%dx, %%al":"=a" (value):"d" (offset));
	return value;
}

uint16_t PLProcessorPrivate::in16(size_t offset) {
	uint16_t value;
	asm volatile("inw %%dx, %%ax":"=a" (value):"d" (offset));
	return value;
}

uint32_t PLProcessorPrivate::in32(size_t offset) {
	uint32_t value;
	asm volatile("in %%dx, %%eax":"=a" (value):"d" (offset));
	return value;
}

void PLProcessorPrivate::out8(uint8_t value, size_t offset) {
	asm volatile("outb %%al, %%dx"::"d" (offset), "a" (value));
}

void PLProcessorPrivate::out16(uint16_t value, size_t offset) {
	asm volatile("outw %%ax, %%dx"::"d" (offset), "a" (value));
}

void PLProcessorPrivate::out32(uint32_t value, size_t offset) {
	asm volatile("out %%eax, %%dx"::"d" (offset), "a" (value));
}

#endif // PLPROCESSOR_PRIVATE_H
